package com.panopset.gui.selection;

import static com.panopset.Util.*;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JToggleButton;

import com.panopset.gui.TPanel;

public abstract class SelectionPanel extends TPanel
implements ActionListener {
    private Vector<SelectionGroup> selectionGroups;
    private Vector<JToggleButton> radioButtons;
    private HashMap<String, String> selections;
    private String key;
    private final Vector<SelectionPanelValueChangeListener> spvcl = new Vector<SelectionPanelValueChangeListener>();
    protected abstract void doSetup();

    public SelectionPanel() {

    }

    public SelectionPanel(String initValue) {
        setValueFromString(initValue);
    }

    public void addSelectionPanelValueChangeListener(SelectionPanelValueChangeListener listener) {
        spvcl.add(listener);
    }

    private Vector<SelectionGroup> getSelectionGroups() {
        if (this.selectionGroups == null) {
            this.selectionGroups = new Vector<SelectionGroup>();
        }
        return this.selectionGroups;
    }

    public void setUp(SelectionGroup group) {
        getSelectionGroups().add(group);
    }

    public void finalizeSetUp() {
        doSetup();
        setLayout(new GridLayout(getSelectionGroups().size(), 1));
        for (SelectionGroup sg : getSelectionGroups()) {
            add(sg);
        }
    }

    public void actionPerformed(ActionEvent e) {
        for (JToggleButton btn : getRadioButtons()) {
            if (e.getSource() == btn) {
                if (btn.isSelected()) {
                    setKey(btn.getText());
                }
            } else {
                if (btn.isSelected()) {
                    btn.setSelected(false);
                }
            }
        }
    }

    public HashMap<String, String> getSelections() {
        if (this.selections == null) {
            this.selections = new HashMap<String, String>();
        }
        return this.selections;
    }

    public Vector<JToggleButton> getRadioButtons() {
        if (this.radioButtons == null) {
            this.radioButtons = new Vector<JToggleButton>();
        }
        return this.radioButtons;
    }

    public String getValue() {
        return getSelections().get(getKey());
    }

    public String getKey() {
        if (this.key == null) {
            this.key = EMPTY_STRING;
        }
        return this.key;
    }

    public void setValueFromString(String newKey) {
        for (JToggleButton btn : getRadioButtons()) {
            if (btn.getText().equals(newKey)) {
                setKey(newKey);
                btn.setSelected(true);
            } else {
                btn.setSelected(false);
            }
        }

    }

    private void setKey(String newKey) {
        this.key = newKey;
        for (SelectionPanelValueChangeListener listener : spvcl) {
            listener.selectionPanelValueHasChangedTo(this.key);
        }
    }

    private static final long serialVersionUID = VERSION_SERIAL_NUMBER;
}
